package com.example.demotool.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.params.SetParams;


public class RedisTest {
    private static Integer inventory = 1001;
    private static final int NUM = 10;
    private SetParams params =SetParams.setParams().nx().px(3);

    public static void main(String[] args) {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.setnx("inventory", "4");
         inventory = Integer.parseInt(jedis.get("inventory"));
        for (int i = 0; i < NUM; i++) {
            new Thread((Runnable) () -> {
                try {
                    Long setnx = jedis.setnx("lock", "test");
                    Thread.sleep(1);
                    if (RedisTest.inventory > 0) {
                        RedisTest.inventory--;
                    }
                    System.out.println(RedisTest.inventory);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}
